package org.bpms.service.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.bpms.model.admin.Sdept;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 后台部门业务处理
 * @author hupei
 *
 */
public class DeptService {
	
	/**
	 * 保存部门
	 * @param dept
	 * @return
	 */
	@Before(Tx.class)
	public Sdept saveOrUpdate(Sdept dept){
		if (null != dept.getStr("id")){
			dept.update();
		}else{
			dept.set("id", UUID.randomUUID().toString().replaceAll("-", ""));
			dept.save();
		}
		return dept;
	}
	
	/**
	 * 递归删除部门
	 * @param id
	 */
	@Before(Tx.class)
	public void delDept(String id){
		Sdept dept = Sdept.me.findById(id);
		if (null != dept){
			List<Sdept> sdepts = Sdept.me.find("select * from sdept where pid=?" , id);
			for (Sdept temp : sdepts){
				this.delDept(temp.getStr("id"));
			}
			Sdept.me.deleteById(id);
			Db.update("delete from sdeptuser where did=?" , id);
		}
	}
	
	/**
	 * 查找所有部门
	 * @param sdept
	 * @param dept
	 */
	public void allDept(List<Sdept> sdept , Sdept dept){
		List<Sdept> lds = null;
		if (null == dept.getStr("id")){
			lds = Sdept.me.find("select a.* , b.realname as leadername from sdept a left join suser b on a.leader=b.id "
					+ "where a.pid=-1  order by a.seq asc");
		}else{
			lds = Sdept.me.find("select a.* , b.realname as leadername from sdept a left join suser b on a.leader=b.id "
					+ "where a.pid=?  order by a.seq asc"
					             , dept.getStr("id"));
		}
		dept.setSdepts(lds);
		if (null != dept.getStr("id") && dept.getStr("pid").equals("-1")){//只加入顶层部门
			sdept.add(dept);
		}
		if (null != lds && lds.size() > 0){
			for (Sdept temp : lds){
				this.allDept(sdept, temp);
			}
		}
	}
	
	/**
	 * 递归保存部门树
	 * @param maptree
	 */
	@Before(Tx.class)
	public void saveTree(Map maptree){
		if (null != maptree.get("pId")){
			Sdept dept = new Sdept();
			dept.set("pid", maptree.get("pId"))
			           .set("text", (String)maptree.get("text"))
			           .set("seq", (Integer)maptree.get("seq"))
			           .set("leader", (String)maptree.get("leader"));
			if (null != maptree.get("id")){
				dept.set("id", maptree.get("id"));
				dept.update();
			}else{
				dept.set("id", UUID.randomUUID().toString().replaceAll("-", ""));
				dept.save();
			}
		}
		if (null != maptree.get("children")){
			List list = (ArrayList) maptree.get("children");
			if (null != list){
				for (int i = 0 ; i < list.size() ; i++){
					Map temp = (Map)list.get(i);
					if (null != temp){
						saveTree(temp);
					}
				}
			}
		}		
	}
	/**
	 * 保存部门用户关系
	 * @param maptree
	 * @param deptId
	 */
	@Before(Tx.class)
	public void updateUserRelation(Map maptree , String deptId){
		if (null != maptree.get("pId") && !maptree.get("id").equals("-1")){
			if ((boolean)maptree.get("checked")){
				List<Record> records = Db.find("select * from sdeptuser where did=? and uid=?",
						                        deptId , maptree.get("id"));
				if (null == records || records.size() == 0){
					Record deptuser = new Record().set("uid", maptree.get("id"))
							.set("did", deptId);
					Db.save("sdeptuser", deptuser);
				}
			}else{
				List<Record> records = Db.find("select * from sdeptuser where did=? and uid=?",
						deptId , maptree.get("id"));
				if (null != records && records.size() > 0){
					Db.deleteById("sdeptuser", records.get(0).getInt("id"));
				}
			}
		}
		if (null != maptree.get("children")){
			List list = (ArrayList) maptree.get("children");
			if (null != list && list.size() > 0){
				for (int i = 0 ; i < list.size() ; i++){
					Map temp = (Map)list.get(i);
					if (null != temp){
						updateUserRelation(temp , deptId);
					}
				}				
			}
		}
	}	
	/**
	 * 查找部门
	 * @return
	 */
	public Sdept findById(String deptId){
		List<Sdept> lds = Sdept.me.find("select a.* , b.realname as leadername from sdept a ,suser b where a.leader=b.id "
				+ "and a.id=?  order by a.seq asc" , deptId);
		if (null != lds && lds.size() > 0){
			return lds.get(0);
		}
		return null;
	}
}
